package com.ar.droid;
import java.io.BufferedReader;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Writer;
import java.util.Scanner;

import javax.swing.*;


import com.sys_con.ColdFusion.*;
import java.awt.Component.*;
public class Main {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		
	  	 System.out.println("1-Manage Keys");
	     System.out.println("2-Encrypt");
		 System.out.println("3-Desencrypt");
		 System.out.println("4-Sign");
		
		 InputStreamReader isr = new InputStreamReader(System.in);
		 BufferedReader br = new BufferedReader(isr);
		 String s = br.readLine();
		 
		 if (s.equalsIgnoreCase("1"))
		 {
			 ManagingKeys mk= new ManagingKeys();
			 System.out.println("1-Generate a new keypair");
			 System.out.println("2-Exporting a public key");
			 System.out.println("3-Importing a public key");
			 System.out.println("4-Verify signatures");
			 System.out.println("5-list PublicKey ");
			 InputStreamReader isr0 = new InputStreamReader(System.in);
			 BufferedReader br0 = new BufferedReader(isr0);
			 String rta = br0.readLine();
			 if (rta.equalsIgnoreCase("1"))
			 {
				   System.out.println("You need a User-ID to identify your key");
				 /*  System.out.println("Please enter a  Real Name");
				   InputStreamReader is = new InputStreamReader(System.in);
				   BufferedReader buf = new BufferedReader(is);
				   String name = buf.readLine();
				   
				   System.out.println("1-Please enter a  comment");
				   InputStreamReader iss = new InputStreamReader(System.in);
				   BufferedReader buff = new BufferedReader(iss);
				   String comment = buff.readLine();
				   
				   System.out.println("1-Please enter a  email address");
				   InputStreamReader is2 = new InputStreamReader(System.in);
				   BufferedReader buf2 = new BufferedReader(is2);
				   String email= buf2.readLine();
				  			   
				   System.out.println("Please specify how long the key should be valid.
	         0 = key does not expire 
	      <n>  = key expires in n days
	      <n>w = key expires in n weeks
	      <n>m = key expires in n months
	      <n>y = key expires in n years");
				   InputStreamReader is3 = new InputStreamReader(System.in);
				   BufferedReader buf3 = new BufferedReader(is3);
				   String expireDate= buf3.readLine();
					
				   
				   System.out.println("You need a Passphrase to protect your private key. Please enter a  passphrase");
				   InputStreamReader is4 = new InputStreamReader(System.in);
				   BufferedReader buf4 = new BufferedReader(is4);
				   String passphrase= buf4.readLine();
				   */
				   String kp=mk.createKeypair("Prueba", "Bx", "ps@gmail.com", "1", "ffas");
				   System.out.println(kp);
				   if (kp.contentEquals("gpg:")) 
				   {
					  //aca si el usuario es el mismo no catchea el error
					  System.out.println("The key was not generate. Please try again. ");
		    
		            }
				   else
		           {
					   System.out.println("The key was generate ");	 
					  	 
		           }
				    
              }//end if=1
			 
			 if (rta.equalsIgnoreCase("2"))
			 {
				 
				 
			// aca va a entregar una lista de claves, deberia hacerse una pantalla donde las muestre
		 //y ver cual agarro, tomando el nombre el comentario y el email asi lo puedo exportar.

				 
				//   String keyID=name+"  "+comment+"  " +email;
				   String keyID= "Prueba Bx ps@gmail.com";
				   String key= mk.exportKey(keyID);
				   Writer output = null;
				   File file = new File(keyID+".txt");
				   output = new BufferedWriter(new FileWriter(file));
				   output.write(key);
				   output.close(); 
					  
					
			 }
			
			 if (rta.equalsIgnoreCase("3"))
			 {
				 //debería abrirse un browser y que elija con extension gpg en la carpeta x defecto gpgDroid
				// File theFile = chooser.getSelectedFile();
				 
				// File theFile = new File("ho.txt");
				
				// ;
				 RandomAccessFile randomAccessFile = null;

                try 
                {   //In this part, the selected file should be opened
                 	 randomAccessFile = new RandomAccessFile("marce", "r");
                 	 //it opens a file and copy its content to be sent it to the importKey
                 	 String content="";
                 	String temp=randomAccessFile.readLine();
               	  	 while (!(temp.contentEquals("-----END PGP PUBLIC KEY BLOCK-----")))
               	  	 {
                            	  content= content+temp+"\n";
                            	    temp=randomAccessFile.readLine();
                      }
               	  	 content=content+temp;
               	  	 
                	  String result= mk.importKey(content);
                     
                    if (result.contentEquals(""))
                    {
                    	
                    	System.out.println("Key successfully imported");
                    }
                    else
                    {
                    	//it prints the error.
                    	System.out.println(result);
                    }
                    	  
                     
                 }//end try
                 
                  
		       catch (FileNotFoundException fileNotFoundException) 
		       {

                    System.err.println("error when open the file.");
                    System.exit(1);

                }//end catch

	       
             } //end if=3
			if (rta.equalsIgnoreCase("4")){
				
				
			 }
			   
				  
				 
			 }
				 
		 }	
	}

